package myzd.config;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import myzd.api.domain.UserIdentity;
import myzd.client.DoctorKbClient;
import myzd.client.GenericTradingServiceClient;
import myzd.client.QiniuClient;
import myzd.client.WeChatQiYeClient;
import okhttp3.OkHttpClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.scheduling.annotation.EnableAsync;
import retrofit2.Retrofit;
import retrofit2.converter.jackson.JacksonConverterFactory;

@Configuration
@EnableAsync
public class DefaultConfiguration {
  @Bean
  public OkHttpClient okHttpClient() {
    return new OkHttpClient.Builder().build();
  }
  public static Retrofit createRetrofit(OkHttpClient okHttpClient, ObjectMapper objectMapper, String host) {
    return new Retrofit.Builder()
      .client(okHttpClient)
      .baseUrl(host)
      .addConverterFactory(JacksonConverterFactory.create(objectMapper))
      .build();
  }
  @Bean
  public ObjectMapper objectMapper() {
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    mapper.registerModule(new JavaTimeModule());
    return mapper;
  }

  @Bean
  @Scope(scopeName = "request", proxyMode = ScopedProxyMode.TARGET_CLASS)
  public UserIdentity auth() {
    return new UserIdentity();
  }

  //各个微服务客户端
  @Bean
  public DoctorKbClient doctorKbClient(OkHttpClient okHttpClient,
                                       ObjectMapper objectMapper,
                                       @Value("${doctor.kb.host}") String host) {
    Retrofit retrofit = createRetrofit(okHttpClient, objectMapper, host);
    return retrofit.create(DoctorKbClient.class);
  }

  @Bean
  public GenericTradingServiceClient genericTradingServiceClient(OkHttpClient okHttpClient,
                                                                 ObjectMapper objectMapper,
                                                                 @Value("${generic.trading.service.host}") String host) {
    Retrofit retrofit = createRetrofit(okHttpClient, objectMapper, host);
    return retrofit.create(GenericTradingServiceClient.class);
  }

  @Bean
  public WeChatQiYeClient weChatQiYeClient(OkHttpClient okHttpClient,
                                           ObjectMapper objectMapper,
                                           @Value("${wechat.qiye.host}") String host) {
    Retrofit retrofit = createRetrofit(okHttpClient, objectMapper, host);
    return retrofit.create(WeChatQiYeClient.class);

  }

  @Bean
  public QiniuClient qiniuClient(OkHttpClient okHttpClient,
                                 ObjectMapper objectMapper,
                                 @Value("${qiniu.service.host}") String host) {
    Retrofit retrofit = createRetrofit(okHttpClient, objectMapper, host);
    return retrofit.create(QiniuClient.class);
  }
}
